```{r}
rm(list = ls())
library(data.table); library(tidyverse); library(marginaleffects); library(afex); library(patchwork); library(hausekeep); library(BayesFactor)
theme_set(theme_minimal())
# source("utils.R")
```

```{r}
d0 <- fread("../data/clean/data-long.csv")
glimpse(d0)
d0 <- d0[veracity != "none"]
d0 <- d0[screenacc == 1.0]
unique(d0$responseid)
# d0[, videorating := (accuracy + reliable) / 2]

summary(d0)


# change column names to resemble contents of topic column
setnames(d0, c("ivermectin", "aspartame", "asymptomatic"), c("ive", "asp", "asymp"))
```

```{r belief}
d0$belief <- -1
d0[topic == "rust", belief := rust]
d0[topic == "ive", belief := ive]
d0[topic == "asp", belief := asp]
d0[topic == "asymp", belief := asymp]
d0[topic == "herd", belief := herd]
d0[topic == "brain", belief := brain]
d0[belief == -1]
dbelief <- select(d0, responseid, topic, condition, belief) |> distinct()
dbelief
```

```{r discernment}
ddiscern <- dcast(d0, responseid + condition ~ veracity, value.var = "videorating")
ddiscern[, discern := true - false]
ddiscern
```

```{r}
d1 <- left_join(ddiscern, dbelief) |> data.table()
d1
d1[, condition := factor(condition, levels = c("misinfo-only", "correction-only", "debunk"), 
                         labels = c("Misinfo", "Correction", "Debunk"))]
```

```{r overall models}
m_discern <- lm(discern ~ condition, d1)
summary(m_discern)
summaryh(m_discern, es = "d")
m_discern_est <- tidy(comparisons(m_discern, variables = list(condition = 'pairwise')))
m_discern_est
m_discern_est$model <- "discern"
m_discern_est$topic <- "All topics"

ttestBF(formula = discern ~ condition, data = d1[condition %in% c("Debunk", "Misinfo")])

m_belief <- lm(belief ~ condition, d1)
summary(m_belief)
summaryh(m_belief, es = "d")
m_belief_est <- tidy(comparisons(m_belief, variables = list(condition = 'pairwise')))
m_belief_est
m_belief_est$model <- "belief"
m_belief_est$topic <- "All topics"

ttestBF(formula = belief ~ condition, data = d1[condition %in% c("Debunk", "Misinfo")])
```

```{r discern models for each topic}
topics <- d1[, unique(topic)]
topics
m_discern_topics <- data.table()
for (ti in 1:length(topics)) {
   dttemp <- d1[topic == topics[ti]]
   m_temp <- lm(discern ~ condition, dttemp)
   m_discern_est_temp <- tidy(comparisons(m_temp, variables = list(condition = 'pairwise')))
   m_discern_est_temp$n <- nrow(dttemp)
   m_discern_est_temp$model <- "discern"
   m_discern_est_temp$topic <- topics[ti]
   m_discern_topics <- bind_rows(m_discern_topics, m_discern_est_temp)
}
m_discern_topics
```


```{r belief models for each topic}
topics <- d1[, unique(topic)]
topics
m_belief_topics <- data.table()
for (ti in 1:length(topics)) {
   dttemp <- d1[topic == topics[ti]]
   m_temp <- lm(belief ~ condition, dttemp)
   m_belief_est_temp <- tidy(comparisons(m_temp, variables = list(condition = 'pairwise')))
   m_belief_est_temp$n <- nrow(dttemp)
   m_belief_est_temp$model <- "belief"
   m_belief_est_temp$topic <- topics[ti]
   m_belief_topics <- bind_rows(m_belief_topics, m_belief_est_temp)
}
m_belief_topics
```

```{r}
models <- bind_rows(m_discern_est, m_belief_est, m_discern_topics, m_belief_topics)
setDT(models)
models
models[, topic := factor(topic, levels = c("rust", "ive", "herd", "asymp", "asp", "brain", "All topics"), 
                         labels = c("Rust Film Set Shooting", "Ivermectin COVID-19 Treatment", "COVID-19 Herd Immunity", "Asymptomatic Transmission", "Aspartame Toxicity", "Left/Right Brain Personality", "All Topics"))]
models[, contrast := factor(contrast, levels = c("Debunk - Misinfo", "Correction - Misinfo", "Debunk - Correction"))]
models <- models[contrast != "Debunk - Correction"]

models$psig <- "ns"
models[p.value <= 0.05, psig := "sig"]

pdiscern <- ggplot(models[model == "discern"], aes(estimate, topic, col = psig)) +
    geom_vline(xintercept = 0, lty = "dashed", col = 'grey') +
    facet_grid(.~contrast, scales = "free_x") +
    geom_point(size = 3) +
    scale_color_manual(values = c("black", "red")) +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high), width = 0, size = 0.8) +
    theme(legend.position = 'none', plot.title = element_text(hjust = 0.5)) +
    labs(x = "", y = "", title = "Subsequent truth discernment")
pdiscern

pbelief <- ggplot(models[model == "belief"], aes(estimate, topic, col = psig)) +
    geom_vline(xintercept = 0, lty = "dashed", col = 'grey') +
    facet_grid(.~contrast, scales = "free_x") +
    geom_point(size = 3) +
    scale_color_manual(values = c("black", "red")) +
    geom_errorbar(aes(xmin = conf.low, xmax = conf.high), width = 0, size = 0.8) +
    theme(legend.position = 'none', plot.title = element_text(hjust = 0.5)) +
    labs(x = "Estimate (95% CI)", y = "", title = "Belief in false claim")
pbelief

pdiscern / pbelief
ggsave("../figures/discern_belief_exclude_inattentive.png", pdiscern / pbelief, dpi = 300, width = 8, height = 5, bg = "white")
```

```{r}

```

